package com.jt.springbootdemomybatis3;

import com.jt.springbootdemomybatis3.mapper.DemoUserMapper;
import com.jt.springbootdemomybatis3.pojo.DemoUser;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class mybatis020 {
    private SqlSessionFactory build;

    @BeforeEach//在执行BeforEach这个注解会提前执行被修饰的方法
    public void init() throws IOException {
        //1.指定资源文件
        String resource = "mybatis/mybatis-config.xml";
        InputStream resourceAsStream = Resources.getResourceAsStream(resource);
        build = new SqlSessionFactoryBuilder().build(resourceAsStream);
    }

    @Test
    public void testMybatis() throws IOException {
        SqlSession sqlSession = build.openSession();
        DemoUserMapper mapper = sqlSession.getMapper(DemoUserMapper.class);
        List<DemoUser> list = mapper.findAll();
        System.out.println(list);
        sqlSession.close();
    }

    @Test
    public void testMybatis2() throws IOException {
        SqlSession sqlSession = build.openSession();
        DemoUserMapper mapper = sqlSession.getMapper(DemoUserMapper.class);
        String name = "王昭君";
        //如果不能保证结果唯一 ,则使用List集合接收数据
        List<DemoUser> list = mapper.findTwo(name);
        System.out.println(list);
        sqlSession.close();
    }

    @Test
    public void testMybatis3() throws IOException {
        SqlSession sqlSession = build.openSession();
        DemoUserMapper mapper = sqlSession.getMapper(DemoUserMapper.class);
//        String sex="女";
//        int age=18;
        Map<String, Object> map = new HashMap<>();
        map.put("sex", "女");
        map.put("age", 18);
        //如果不能保证结果唯一 ,则使用List集合接收数据
        List<DemoUser> list = mapper.findThree(map);
        System.out.println(list);
        sqlSession.close();
    }

    //按照指定排序
    @Test
    public void testMybatis4() throws IOException {
        SqlSession sqlSession = build.openSession();
        DemoUserMapper mapper = sqlSession.getMapper(DemoUserMapper.class);
        String column = "age";
        List<DemoUser> list = mapper.findOrder(column);
        //如果不能保证结果唯一 ,则使用List集合接收数据
        System.out.println(list);
        sqlSession.close();
    }

    //添加
    //关于事物的说明:
    /*mybatis中的更新操作默认事物都是 开启的,如果进行更新操作*/
    @Test
    public void testMybatis5()  {
        SqlSession sqlSession = build.openSession();
        DemoUserMapper mapper = sqlSession.getMapper(DemoUserMapper.class);
        //数据库主键自增,所以主键ID可以为null
        DemoUser user = new DemoUser(null,"富哦",99,"女");
        int rows=mapper.saveUser(user);
        if (rows>0){
            System.out.println("影响的行数"+rows);
            //事务提交
            sqlSession.commit();
        }

        sqlSession.close();

    }
    //修改
//    @Test
//    public void testMybatis6()  {
//        SqlSession sqlSession = build.openSession();
//        DemoUserMapper mapper = sqlSession.getMapper(DemoUserMapper.class);
//        //数据库主键自增,所以主键ID可以为null
//        DemoUser user = new DemoUser(1,"首山大士",5000,"男");
//       int rows= mapper.saveUpdate(user);
//        System.out.println(rows);
//
//        sqlSession.close();
//
//    }
    //删除
//    @Test
//    public void testMybatis7()  {
//        SqlSession sqlSession = build.openSession();
//        DemoUserMapper mapper = sqlSession.getMapper(DemoUserMapper.class);
//        String name="富哦";
//        //如果不能保证结果唯一 ,则使用List集合接收数据
//        DemoUser d = mapper.findD(name);
//        System.out.println(d);
//        sqlSession.close();
//    }
}